package com.gateway.util;

import java.math.BigDecimal;

public class MathTool {

  // 计算百分比(保留2位并四舍五入)
  public static double calc(double area, double total) {
    double result;
    if(area == 0 || total == 0) {
      result=0;
    } else {
      result=area / total;
    }
    int scale=2;// 设置位数
    int roundingMode=4;// 表示四舍五入，可以选择其他舍值方式，例如去尾，等等.
    BigDecimal bd=new BigDecimal((double)result * 100);
    bd=bd.setScale(scale, roundingMode);
    result=bd.doubleValue();
    return result;
  }

  public static float calc(float area, float total) {
    float result;
    if(area == 0 || total == 0) {
      result=0;
    } else {
      result=area / total;
    }
    int scale=2;
    int roundingMode=4;
    BigDecimal bd=new BigDecimal((float)result * 100);
    bd=bd.setScale(scale, roundingMode);
    result=bd.floatValue();
    return result;
  }

  // 保留2位并四舍五入
  public static double calc(double value) {
    int scale=2;
    int roundingMode=4;
    BigDecimal bd=new BigDecimal((double)value);
    bd=bd.setScale(scale, roundingMode);
    value=bd.doubleValue();
    return value;
  }
}
